home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevbmpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.7 KB  |  235 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevbmpc.c,v 1.2 2000/09/19 19:00:11 lpd Exp $ */
  20. /* .BMP file format driver utilities */
  21. #include "gdevprn.h"
  22. #include "gdevbmp.h"
  23.  
  24. /*
  25.  * Define BMP file format structures.
  26.  * All multi-byte quantities are stored LSB-first!
  27.  */
  28.  
  29. typedef ushort word;
  30. #if arch_sizeof_int == 4
  31. typedef uint dword;
  32. #else
  33. #  if arch_sizeof_long == 4
  34. typedef ulong dword;
  35. #  endif
  36. #endif
  37. #if arch_is_big_endian
  38. #  define BMP_ASSIGN_WORD(a,v) a = ((v) >> 8) + ((v) << 8)
  39. #  define BMP_ASSIGN_DWORD(a,v)\
  40.      a = ((v) >> 24) + (((v) >> 8) & 0xff00L) +\
  41.      (((dword)(v) << 8) & 0xff0000L) + ((dword)(v) << 24)
  42. #else
  43. #  define BMP_ASSIGN_WORD(a,v) a = (v)
  44. #  define BMP_ASSIGN_DWORD(a,v) a = (v)
  45. #endif
  46.  
  47. typedef struct bmp_file_header_s {
  48.  
  49.     /* BITMAPFILEHEADER */
  50.  
  51.     /*
  52.      * This structure actually begins with two bytes
  53.      * containing the characters 'BM', but we must omit them,
  54.      * because some compilers would insert padding to force
  55.      * the size member to a 32- or 64-bit boundary.
  56.      */
  57.  
  58.     /*byte  typeB, typeM; *//* always 'BM' */
  59.     dword size;            /* total size of file */
  60.     word reserved1;
  61.     word reserved2;
  62.     dword offBits;        /* offset of bits from start of file */
  63.  
  64. } bmp_file_header;
  65.  
  66. #define sizeof_bmp_file_header (2 + sizeof(bmp_file_header))
  67.  
  68. typedef struct bmp_info_header_s {
  69.  
  70.     /* BITMAPINFOHEADER */
  71.  
  72.     dword size;            /* size of info header in bytes */
  73.     dword width;        /* width in pixels */
  74.     dword height;        /* height in pixels */
  75.     word planes;        /* # of planes, always 1 */
  76.     word bitCount;        /* bits per pixel */
  77.     dword compression;        /* compression scheme, always 0 */
  78.     dword sizeImage;        /* size of bits */
  79.     dword xPelsPerMeter;    /* X pixels per meter */
  80.     dword yPelsPerMeter;    /* Y pixels per meter */
  81.     dword clrUsed;        /* # of colors used */
  82.     dword clrImportant;        /* # of important colors */
  83.  
  84.     /* This is followed by (1 << bitCount) bmp_quad structures, */
  85.     /* unless bitCount == 24. */
  86.  
  87. } bmp_info_header;
  88.  
  89. typedef struct bmp_quad_s {
  90.  
  91.     /* RGBQUAD */
  92.  
  93.     byte blue, green, red, reserved;
  94.  
  95. } bmp_quad;
  96.  
  97. /* Write the BMP file header. */
  98. private int
  99. write_bmp_depth_header(gx_device_printer *pdev, FILE *file, int depth,
  100.                const byte *palette /* [4 << depth] */,
  101.                int raster)
  102. {
  103.     /* BMP scan lines are padded to 32 bits. */
  104.     ulong bmp_raster = raster + (-raster & 3);
  105.     int height = pdev->height;
  106.     int quads = (depth <= 8 ? sizeof(bmp_quad) << depth : 0);
  107.  
  108.     /* Write the file header. */
  109.  
  110.     fputc('B', file);
  111.     fputc('M', file);
  112.     {
  113.     bmp_file_header fhdr;
  114.  
  115.     BMP_ASSIGN_DWORD(fhdr.size,
  116.              sizeof_bmp_file_header +
  117.              sizeof(bmp_info_header) + quads +
  118.              bmp_raster * height);
  119.     BMP_ASSIGN_WORD(fhdr.reserved1, 0);
  120.     BMP_ASSIGN_WORD(fhdr.reserved2, 0);
  121.     BMP_ASSIGN_DWORD(fhdr.offBits,
  122.              sizeof_bmp_file_header +
  123.              sizeof(bmp_info_header) + quads);
  124.     if (fwrite((const char *)&fhdr, 1, sizeof(fhdr), file) != sizeof(fhdr))
  125.         return_error(gs_error_ioerror);
  126.     }
  127.  
  128.     /* Write the info header. */
  129.  
  130.     {
  131.     bmp_info_header ihdr;
  132.  
  133.     BMP_ASSIGN_DWORD(ihdr.size, sizeof(ihdr));
  134.     BMP_ASSIGN_DWORD(ihdr.width, pdev->width);
  135.     BMP_ASSIGN_DWORD(ihdr.height, height);
  136.     BMP_ASSIGN_WORD(ihdr.planes, 1);
  137.     BMP_ASSIGN_WORD(ihdr.bitCount, depth);
  138.     BMP_ASSIGN_DWORD(ihdr.compression, 0);
  139.     BMP_ASSIGN_DWORD(ihdr.sizeImage, bmp_raster * height);
  140.     /*
  141.      * Earlier versions of this driver set the PelsPerMeter values
  142.      * to zero.  At a user's request, we now set them correctly,
  143.      * but we suspect this will cause problems other places.
  144.      */
  145. #define INCHES_PER_METER (100 /*cm/meter*/ / 2.54 /*cm/inch*/)
  146.     BMP_ASSIGN_DWORD(ihdr.xPelsPerMeter,
  147.              (dword)(pdev->x_pixels_per_inch * INCHES_PER_METER));
  148.     BMP_ASSIGN_DWORD(ihdr.yPelsPerMeter,
  149.              (dword)(pdev->y_pixels_per_inch * INCHES_PER_METER));
  150. #undef INCHES_PER_METER
  151.     BMP_ASSIGN_DWORD(ihdr.clrUsed, 0);
  152.     BMP_ASSIGN_DWORD(ihdr.clrImportant, 0);
  153.     if (fwrite((const char *)&ihdr, 1, sizeof(ihdr), file) != sizeof(ihdr))
  154.         return_error(gs_error_ioerror);
  155.     }
  156.  
  157.     /* Write the palette. */
  158.  
  159.     if (depth <= 8)
  160.     fwrite(palette, sizeof(bmp_quad), 1 << depth, file);
  161.  
  162.     return 0;
  163. }
  164.  
  165. /* Write the BMP file header. */
  166. int
  167. write_bmp_header(gx_device_printer *pdev, FILE *file)
  168. {
  169.     int depth = pdev->color_info.depth;
  170.     bmp_quad palette[256];
  171.  
  172.     if (depth <= 8) {
  173.     int i;
  174.     gx_color_value rgb[3];
  175.     bmp_quad q;
  176.  
  177.     q.reserved = 0;
  178.     for (i = 0; i != 1 << depth; i++) {
  179.         (*dev_proc(pdev, map_color_rgb))((gx_device *)pdev,
  180.                          (gx_color_index)i, rgb);
  181.         q.red = gx_color_value_to_byte(rgb[0]);
  182.         q.green = gx_color_value_to_byte(rgb[1]);
  183.         q.blue = gx_color_value_to_byte(rgb[2]);
  184.         palette[i] = q;
  185.     }
  186.     }
  187.     return write_bmp_depth_header(pdev, file, depth, (const byte *)palette,
  188.                   gdev_prn_raster(pdev));
  189. }
  190.  
  191. /* Write a BMP header for separated CMYK output. */
  192. int
  193. write_bmp_separated_header(gx_device_printer *pdev, FILE *file)
  194. {
  195.     int depth = pdev->color_info.depth;
  196.     int plane_depth = depth / 4;
  197.     bmp_quad palette[256];
  198.     bmp_quad q;
  199.     int i;
  200.  
  201.     q.reserved = 0;
  202.     for (i = 0; i < 1 << plane_depth; i++) {
  203.     q.red = q.green = q.blue =
  204.         255 - i * 255 / ((1 << plane_depth) - 1);
  205.     palette[i] = q;
  206.     }
  207.     return write_bmp_depth_header(pdev, file, plane_depth,
  208.                   (const byte *)palette,
  209.                   bitmap_raster(pdev->width * plane_depth));
  210. }
  211.  
  212. /* 24-bit color mappers (taken from gdevmem2.c). */
  213. /* Note that Windows expects RGB values in the order B,G,R. */
  214.  
  215. /* Map a r-g-b color to a color index. */
  216. gx_color_index
  217. bmp_map_16m_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  218.           gx_color_value b)
  219. {
  220.     return gx_color_value_to_byte(r) +
  221.     ((uint) gx_color_value_to_byte(g) << 8) +
  222.     ((ulong) gx_color_value_to_byte(b) << 16);
  223. }
  224.  
  225. /* Map a color index to a r-g-b color. */
  226. int
  227. bmp_map_16m_color_rgb(gx_device * dev, gx_color_index color,
  228.           gx_color_value prgb[3])
  229. {
  230.     prgb[2] = gx_color_value_from_byte(color >> 16);
  231.     prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
  232.     prgb[0] = gx_color_value_from_byte(color & 0xff);
  233.     return 0;
  234. }
  235.